package com.cu.employmentmanagementsystem.handler;

import com.cu.employmentmanagementsystem.common.enums.WxChatMsgTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WxChatMsgHandlerFactory implements InitializingBean
{
    @Resource
    private List<WxChatMsgHandler> wxChatMsgHandlerList;

    private Map<WxChatMsgTypeEnum,WxChatMsgHandler> wxChatMsgHandlerMap = new HashMap<>();

    public WxChatMsgHandler getHandlerByMsgType(String msgType)
    {
        WxChatMsgTypeEnum wxChatMsgTypeEnum = WxChatMsgTypeEnum.getEnumByType(msgType);
        return wxChatMsgHandlerMap.get(wxChatMsgTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        for (WxChatMsgHandler wxChatMsgHandler : wxChatMsgHandlerList)
        {
            WxChatMsgTypeEnum wxChatMsgType = wxChatMsgHandler.getChatMsgType();
            wxChatMsgHandlerMap.put(wxChatMsgType,wxChatMsgHandler);
        }
    }
}
